<template>
  <div>
    <van-nav-bar
      title="收获地址"
      left-arrow
      @click-left="onClickLeft('myView')" />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit" />
    <!-- @add="onAdd('AddressEdit',id)" -->
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      chosenAddressId: 0,
      list: [],
    };
  },
  created() {
    this.loadData();

    //生命周期  创建后要做的事情  vue被创建时候执行的
  },
  methods: {
    loadData() {
      //不是直接使用axios 而是使用$http
      //baseURL="http://localhost:8080"
      var _this = this;
      this.$http
        .get("/address/queryAll?id=" + this.$route.query.id)
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            //this不可以直接使用 代表的是axios对象
            let listA = response.data.data;

            // alert(JSON.stringify(listA))
            for (var i = 0; i < listA.length; i++) {
              let b = false;
              if (listA[i].isDefault == "1") {
                b = true;
                _this.chosenAddressId = listA[i].addressId;
              }
              let a = {
                id: listA[i].addressId,
                name: listA[i].addressName,
                tel: listA[i].phone,
                address: listA[i].detail,
                isDefault: b,
                userId: listA[i].userId,
              };
              _this.list.push(a);
            }
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    onClickLeft(uri) {
      this.$router.push(
        uri,
        () => {},
        () => {}
      );
    },
    onAdd() {
      //  this.$router.push({ name: uri, query: { userId: id } });
      this.$router.push({
        name: "AddressEdit",
        query: { UserId: this.list[0].userId },
      });
    },
    onEdit(item, index) {
      let id = this.list[index].id;
      this.$router.push({
        name: "AddressEdit",
        query: { id: id, UserId:this.list[0].userId },
      });
    },
  },
  components: {
    // 组件注册的地方 相当于pom文件
  },
};
</script>
<style></style>
